package cn.xspace.wnx.view.wnxchat;

import android.content.Context;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.util.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.xspace.library.util.MsgType;
import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.Body;
import cn.xspace.chatui.bean.MessageEntity;
import cn.xspace.wnx.entity.WNXBean;
import cn.xspace.wnx.util.Navigator;

/**
 * Created by liuchen on 2016/11/16.
 */

public class WnxMsgChatRow extends BaseWnxChatRow {
    protected LinearLayout subLayout;
    private ImageView logoImg;
    private TextView titleTxt;
    private TextView contentTxt;
    // 时间戳
    protected TextView timeStampView;
    private LinearLayout firstView;
    private TextView kindTxt;
    private Map<View, String> viewMap;


    public WnxMsgChatRow(Context context, WNXBean bean, int position, BaseAdapter adapter) {
        super(context, bean, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(R.layout.listitem_wnx_org, this);
    }

    @Override
    protected void onFindViewById() {
        timeStampView = (TextView) findViewById(R.id.timestamp);
        subLayout = (LinearLayout) this.findViewById(R.id.linear_sub);
        logoImg = (ImageView) findViewById(R.id.img_logo);
        titleTxt = (TextView) findViewById(R.id.txt_title);
        contentTxt = (TextView) findViewById(R.id.txt_content);
        firstView = (LinearLayout) findViewById(R.id.linear_first);
        kindTxt = (TextView) findViewById(R.id.txt_kind);
        viewMap = new HashMap<>();
    }

    @Override
    protected void onUpdateView() {
    }

    @Override
    protected void setClickListener() {
        super.setClickListener();
    }

    OnClickListener itemOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getTag() != null) {
                if (v.getTag() instanceof Body.NewssnapBean) {
                    Body.NewssnapBean newssnapBean = (Body.NewssnapBean) v.getTag();
                    Navigator.startWnxWebView(context, newssnapBean.getSrc(),"", wnxBean
                            .msg_sender_id, MsgType.WNX_MSG.toNumber());
                }
            }
        }
    };

    @Override
    protected void onSetUpView() {
        LogUtil.d("wnxBean>>" + wnxBean.toString());
        kindTxt.setText(wnxBean.msg_kind);
        try {
            JSONArray jsonArray = null;
//            if (wnxBean.isHistory) {
//                JSONObject jsonObject = new JSONObject(wnxBean.content);
//                jsonArray = jsonObject.optJSONArray("newssnap");
//            } else {
            jsonArray = new JSONArray(wnxBean.content);
//            }
            LogUtil.d("jsonArray >>" + jsonArray.toString());
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject jsonObject = jsonArray.optJSONObject(j);
                Body.NewssnapBean newBean = new Body.NewssnapBean(jsonObject);
                if (j == 0) {
                    titleTxt.setText(newBean.getTitle());
                    contentTxt.setText(newBean.getSummary());
                    HeadShowUtil.headShow(context, newBean.getImg(), "1", logoImg, HeadShowUtil.HeadType.WnxLogo);
                    if (firstView != null) {
                        LogUtil.d("firstView!=null");
                        firstView.setTag(newBean);
                        firstView.setOnClickListener(itemOnClickListener);
                    }
                } else {
                    View secondView = inflater.inflate(R.layout.listitem_org_title, null);
                    ((TextView) secondView.findViewById(R.id.txt_title)).setText(newBean.getTitle());
                    ((TextView) secondView.findViewById(R.id.txt_kind)).setText(wnxBean.msg_kind);
                    secondView.setTag(newBean);
                    secondView.setOnClickListener(itemOnClickListener);
                    HeadShowUtil.headShow(context, newBean.getImg(), "1", ((ImageView) secondView.findViewById(R.id
                            .img_logo)), HeadShowUtil.HeadType.WnxLogo);
                    subLayout.addView(secondView);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onBubbleClick() {
    }
}
